﻿using SocketTestClient.Sender;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SocketTestClient.RequestObject
{
    public class TestRequest : IRequest
    {
        public RequestType OrderType = RequestType.TestRequest;
        public double doubleTest = 0.5;
        public int intTest = 1234567;
        public string stringTest = "0123456789ABCDEFGH";
        public string longStringTest;

        public int retType;
        public double retDouble;
        public int retInt;
        public string retString;
        public string retLongString;


        private SendOrderResult _result;
        private byte[] _sndData;

        public TestRequest()
        {
            DataSendBuffer sb = new DataSendBuffer();
            sb.Add((int)OrderType);
            sb.Add(doubleTest);
            sb.Add(intTest);
            sb.Add(stringTest);

            longStringTest = "";
            for (int i = 0; i < 1024 * 20; i++)
            {
                longStringTest += i + ": " + stringTest + "\r\n";
            }
            sb.Add(longStringTest);
            _sndData = sb.GetBytes();
        }

        public byte[] GetBytes()
        {
            // Clear Result
            _result = SendOrderResult.None;

 
            return _sndData;
        }

        public void FromBytes(byte[] data, int length)
        {
            DataRcvBuffer rb = new DataRcvBuffer(data, length);
            retType = rb.GetInt();
            retDouble = rb.GetDouble();
            retInt = rb.GetInt();
            retString = rb.GetString();
            retLongString = rb.GetString();

            bool isOK = true;
            do
            {
                if (retType != (int)OrderType) { isOK = false; break; }
                if (retDouble != doubleTest) { isOK = false; break; }
                if (retInt != intTest) { isOK = false; break; }
                if (retLongString.CompareTo(longStringTest) != 0) { isOK = false; break; }
                if (retString.CompareTo(stringTest) != 0) { isOK = false; break; }
            } while (false);
            if (isOK == false)
            {
                System.Console.WriteLine("TestRequest: Error!");
                _result = SendOrderResult.NG;
            }
            else
            {
                System.Console.WriteLine("TestRequest: OK!");
                _result = SendOrderResult.OK;
            }
        }

        public SendOrderResult Result
        {
            get { return _result; }
        }
    }
}
